<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="button" value="读取文件" onclick="read()"/>
<div id="result">
    <!--读取到的文件内容-->
</div>
<script>
    function getData(filename) {
        return new Promise((resolve, reject) => {
            let objXmlHttp = new XMLHttpRequest();
            objXmlHttp.open("GET", filename, true);
            objXmlHttp.onreadystatechange = () => {
                if (objXmlHttp.readyState === 4) {
                    if (objXmlHttp.status === 200) {
                        resolve(objXmlHttp.responseText);
                    }
                    else {
                        reject("读取文件失败");
                    }
                }
            };
            objXmlHttp.send();
        });
    }
    function read() {
        async function get() {
            const filename = "promise对象测试文件1.txt";
            const result = getData(filename).catch((error) => {
                throw new Error(error);
            });
            return result;
        }
        get().then(response => {
            let result = document.getElementById("result");
            result.innerHTML = response;
        }).catch(err => console.log(err));
    }
</script>
</body>
</html>